package com.gy.api.base;

import com.gy.util.RsaUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.security.PrivateKey;
import java.security.PublicKey;

@Data
@Configuration
public class RsaKeyProperties {

    @Value("${reakey.pubKeyFile}")
    private String pubKeyFile;

    @Value("${reakey.priKeyFile}")
    private String priKeyFile;

    private PublicKey publicKey;
    private PrivateKey privateKey;

    /**
     * 系统启动的时候触发，将公钥文件从本机文件加载为公私钥对象
     * @throws Exception 公私钥加载异常
     */
    @PostConstruct
    public void createRsaKey() throws Exception {
        publicKey = RsaUtils.getPublicKey(pubKeyFile);
        privateKey = RsaUtils.getPrivateKey(priKeyFile);
    }
}
